package org.admin.entity.dto;

import core.validate.Rule;
import lombok.AccessLevel;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import org.admin.entity.BaseDto;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Data
public class PowerDto extends BaseDto<PowerDto> {
    protected PowerDto inst() { return this; }

    public PowerDto(){
        addRule(new Rule[] {
                Rule.Number("roleId").min(1, "指定角色非法"),
                Rule.Array("powers").onPreCheck(val -> {
                    List<Power> value = new ArrayList<>();
                    if(val instanceof List){
                        ((List<?>) val).forEach(item -> {
                            if(item instanceof Map){
                                if(((Map<?, ?>) item).containsKey("groupId") && ((Map<?, ?>) item).containsKey("power")){
                                    value.add(new Power(){{
                                        setGroupId((Integer) ((Map<?, ?>)item).get("groupId"));
                                        setPower((Integer) ((Map<?, ?>)item).get("power"));
                                    }});
                                }
                            }
                        });
                    }
                    return value;
                })
        });

    }

    private Integer roleId;

    @Getter(AccessLevel.NONE)
    private List<Power> powers;

    public List<Power> getPowers(){
        if(powers == null)
            powers = new ArrayList<>();
        return powers;
    }


}
